<?php
class Loan_JsonController extends Zend_Controller_Action
{
    
    public function init()
    {
        $this->getHelper('layout')->setLayout('json');
    }
    
    public function getLoanProductParamsAction()
    {
        require_once 'LoanProducts.php';
        $products = new LoanProducts();
        try {
            $product = $products->getRecord((int) $this->_request->getParam('id'))->d;
            $data = array(
                'Name' => $product['Name'],
                'InterestCalculation' => $product['InterestCalculation'],
                'CalendarType' => $product['CalendarType'],
                'PaymentFrequency' => $product['PaymentFrequency'],
                'Installments' => $product['Installments'],
                'AnnualPercentageRate' => $product['AnnualPercentageRate'],
                'LoanInsurancePct' => $product['LoanInsurancePct'],
                'HealthInsurancePct' => $product['HealthInsurancePct'],
                'AdministrativeFeePct' => $product['AdministrativeFeePct'],
                'PrincipalGraceRate' => $product['PrincipalGraceRate'],
                'PrincipalGraceMargin' => $product['PrincipalGraceMargin'],
                'DelayGrace' => $product['DelayGrace'],
                'DelayPenaltyRate' => $product['DelayPenaltyRate'],
                'DelayPenaltyFixedCharge' => $product['DelayPenaltyFixedCharge']
                );
            echo $this->_encode($data);  
        } catch (Exception $e) {
            echo $this->_encode(array('err' => $e->getMessage()));
        }
        exit;
    } 
    
    protected function _encode($data)
    {
        $options = array();
        $json = '[';
        foreach ($data as $oV => $oT) { $oT = addslashes($oT); $options[] = "{oV: '$oV', oT: '$oT'}"; }
        $json .= implode(',', $options);
        $json .= ']';
        return $json;        
    }
}